//
// Created by fh on 2021/6/1.
//

#include "SqStack.h"

/* 插入元素e为新的栈顶元素 */
Status SqStackPush(SqStack *s,int e) {
    // 栈满
    if (s->top == MAXSIZE - 1) {
        return ERROR;
    }
    s->top++;
    s->data[s->top] = e;
    return OK;
}

/* 若栈不空，则删除s的栈顶元素，用e返回其值，并返回OK，否则返回ERROR */
Status SqStackPop(SqStack *s,int *e) {
    if (s->top == -1) {
        return ERROR;
    }
    *e = s->data[s->top];
    s->top--;
    return OK;
}

/* 遍历顺序栈 */
Status SqStackDisplay(SqStack s) {
    if (s.top == -1) {
        printf("空栈\n");
        return ERROR;
    }
    printf("遍历栈: ");
    for (int i = 0; i <= s.top; ++i) {
        printf("%d ",s.data[i]);
    }
    printf("\n");
    printf("top: %d\n",s.top);
    return OK;
}

/* 测试demo */
void sqStackTestDemo() {

    {
        SqStack *l = (SqStack *)malloc(sizeof(SqStack));
        l->top = -1;

        printf("--------------------------- 数组栈进栈 ------------------------\n");
        Status result = SqStackPush(l,4);
        printf("SqStackPush result : %d\n",result);
        SqStackDisplay(*l);

        result = SqStackPush(l,56);
        printf("SqStackPush result : %d\n",result);
        SqStackDisplay(*l);

        result = SqStackPush(l,8);
        printf("SqStackPush result : %d\n",result);
        SqStackDisplay(*l);

        result = SqStackPush(l,12);
        printf("SqStackPush result : %d\n",result);
        SqStackDisplay(*l);

        printf("--------------------------- 数组栈出栈 ------------------------\n");
        int a = 0;
        result = SqStackPop(l,&a);
        printf("SqStackPop result : %d\n",result);
        printf("a : %d\n",a);
        SqStackDisplay(*l);

        result = SqStackPop(l,&a);
        result = SqStackPop(l,&a);
        SqStackDisplay(*l);

    }
}


/* 插入元素e为新的栈顶元素，stackNumber用来判断是栈1还是栈2 */
Status SqDoubleStackPush(SqDoubleStack *s,int e,int stackNumber) {
    /* 栈已满,不能再push新元素了 */
    if (s->top1+1 == s->top2) {
        return ERROR;
    }
    if (stackNumber == 1) { /* 栈1有元素进栈 */
        s->data[++s->top1] = e; /* 若栈1则先top1+1后给数组元素赋值 */
    } else if (stackNumber == 2) { /* 栈2有元素进栈 */
        s->data[--s->top2] = e; /* 若栈2则先top2-1后给数组元素赋值 */
    }
    return OK;
}

/* 若栈不空，则删除s的栈顶元素，用e返回其值，并返回OK，否则返回ERROR */
Status SqDoubleStackPop(SqDoubleStack *s,int *e,int stackNumber) {
    if (stackNumber == 1) {
        if (s->top1 == -1) {
            return ERROR; /* 说明栈1已经是空栈，溢出 */
        }
        *e = s->data[s->top1--]; /* 将栈1的栈顶元素出栈 */
    } else if (stackNumber == 2) {
        if (s->top2 == MAXSIZE) {
            return ERROR; /* 说明栈2已经是空栈，溢出 */
        }
        *e = s->data[s->top2++]; /* 将栈2的栈顶元素出栈 */
    }
    return OK;
}

/* 遍历共享顺序栈 */
Status SqDoubleStackDisplay(SqDoubleStack s) {
    if (s.top1 == -1 && s.top2 == MAXSIZE) {
        printf("空栈\n");
        return ERROR;
    }
    printf("遍历栈1: ");
    for (int i = 0; i <= s.top1; ++i) {
        printf("%d ",s.data[i]);
    }
    printf("\n");

    printf("遍历栈2: ");
    for (int i = MAXSIZE-1; i >= s.top2; --i) {
        printf("%d ",s.data[i]);
    }
    printf("\n");

    printf("遍历整个数组共享栈: ");
    for (int i = 0; i < MAXSIZE; ++i) {
        printf("%d ",s.data[i]);
    }
    printf("\n");

    return OK;
}

/* 测试demo */
void sqDoubleStackTestDemo() {
    SqDoubleStack *l = (SqDoubleStack *) malloc(sizeof(SqDoubleStack));
    l->top1 = -1;
    l->top2 = MAXSIZE;
    printf("--------------------------- 共享数组栈进栈 ------------------------\n");
    Status result = SqDoubleStackPush(l,1,1);
    printf("SqDoubleStackPop result : %d\n",result);
    SqDoubleStackDisplay(*l);

    result = SqDoubleStackPush(l,2,2);
    printf("SqDoubleStackPop result : %d\n",result);
    SqDoubleStackDisplay(*l);

    result = SqDoubleStackPush(l,3,1);
    printf("SqDoubleStackPop result : %d\n",result);
    SqDoubleStackDisplay(*l);

    result = SqDoubleStackPush(l,4,2);
    printf("SqDoubleStackPop result : %d\n",result);
    SqDoubleStackDisplay(*l);

    result = SqDoubleStackPush(l,5,1);
    printf("SqDoubleStackPop result : %d\n",result);
    SqDoubleStackDisplay(*l);

    result = SqDoubleStackPush(l,6,1);
    printf("SqDoubleStackPop result : %d\n",result);
    SqDoubleStackDisplay(*l);

    result = SqDoubleStackPush(l,7,2);
    printf("SqDoubleStackPop result : %d\n",result);
    SqDoubleStackDisplay(*l);

    printf("--------------------------- 共享数组栈出栈 ------------------------\n");
    int a = 0;
    result = SqDoubleStackPop(l,&a,1);
    printf("SqDoubleStackPop result : %d\n",result);
    printf("a : %d\n",a);
    SqDoubleStackDisplay(*l);

    result = SqDoubleStackPop(l,&a,1);
    printf("SqDoubleStackPop result : %d\n",result);
    printf("a : %d\n",a);
    SqDoubleStackDisplay(*l);

}